global class ApexSchedulerSendMessageMSF implements schedulable{

        global void execute(schedulablecontext sc){
          if(date.today().day()!=25)
          MainShelfDisplayAchievement();
     //     if(date.today().day()==25)
      //    sandmessage();
        }
        public void MainShelfDisplayAchievement(){
         
         /*
         2010-12-15 modify by lisiheng;
         以门店为单位组成数据，最后一次拜访时达标为1，否则为0；
         */
         list<in_store_visit__c> isvl=[select id,ownerid__c,visit_date__c,MainShelfDisplayIsStandard__c,OutletName_del__c from in_store_visit__c 
                                                where HasRun_MainShelfDisplayAchievement__c=false and LastDistributionDate__c !=:null limit 4000];
         list<TradeDashboardDetails__c> upsertTDD=new list<TradeDashboardDetails__c>();
         integer actual;
         for(in_store_visit__c i:isvl){
             actual=0;
             if(i.MainShelfDisplayIsStandard__c){
                 actual=1;
             }
             TradeDashboardDetails__c tdd=new TradeDashboardDetails__c(CriticalEnabler__c='Main Shelf Display Achievement',
                                                    actual__c=actual,tsr__c=i.OwnerId__c,outlet__c=i.outletname_del__c,
                                                    ScoreStandard__c=ScoreStandard__c.getValues('Main Shelf Display Achievement').Standard__c,
                                                    ownerid = i.ownerid__c,salesmonth__c=i.visit_date__c.toStartOfMonth(),
                                                    PrimaryKeyId__c='Main Shelf Display Achievement'+i.OutletName_del__c+i.visit_date__c.tostartofmonth()
                                                    );
             upsertTdd.add(tdd); 
             i.HasRun_MainShelfDisplayAchievement__c=true;                                      
         }
         upsert upsertTDD primarykeyid__c;
         update isvl;
         upserttdd.clear();
         isvl.clear();
    }
    /*
    public void sandmessage(){
                    profile pname=[select name,id from profile where name=:'Sales Rep - Medical'];
            list<AggregateResult> cpl=[select count(id),Monthly_Call_Plan__r.ownerid oid from callplan__c where (Monthly_Call_Plan__r.PlanMonth__c =:(date.today().month()+1)+'月' and Monthly_Call_Plan__r.Planyear__c=:date.today().year()+'') or (Monthly_Call_Plan__r.PlanMonth__c =:'1月' and Monthly_Call_Plan__r.Planyear__c=:(date.today().year()+1)+'') group by Monthly_Call_Plan__r.ownerid,Monthly_Call_Plan__r.manager__c];
            list<id> idl=new list<id>();
            list<id> midl=new list<id>();
            for(AggregateResult cp:cpl){
                idl.add(string.valueof(cp.get('oid')));
            }
            
            list<user> u=[select id,managerid,lastname,firstname,Alias from user where profileid=:pname.id and id not in:idl];
            
            Message__c m=new Message__c();
            ApexPages.StandardController con= new ApexPages.StandardController(m);
            SendMessage sm=new SendMessage(con);
            sm.mhr.Name='您尚未提交下月拜访计划';
            sm.mhr.Content__c='您好，您下月的拜访计划为空，请尽快提交下月拜访计划，谢谢合作。';
            for(user ul:u){
              sm.accRightList.add(new MessageEntry(true,ul));
              midl.add(ul.managerid);
            }
            sm.SendAll();
            list<user> mu=[select id from user where id in:midl and IsActive =: true];
            
            Message__c m2=new Message__c();
            ApexPages.StandardController con2= new ApexPages.StandardController(m2);
            SendMessage msm;
            for(user mul:mu){
                for(user ul:u){
                    if(ul.managerid==mul.id){
                      msm=new SendMessage(con2);
                      msm.mhr.Name='您下属MSR:'+ul.Alias+'尚未提交下月拜访计划';
                      msm.mhr.Content__c='您好，'+ul.Alias+'下月的拜访计划为空，请确认，谢谢。';
                      msm.accRightList.add(new MessageEntry(true,mul)); 
                      msm.SendAll();
                      msm.accRightList.clear();
                     }
                      
                 }
                
            }     
    }*/
}